//
//  ClubGameWelfareViewController.swift
//  77Club
//
//  Created by Apple on 2024/11/6.
//

import UIKit
import JXSegmentedView


class ClubGameWelfareViewController: ClubBaseViewController {

    private var topView = ClubGameWelfareTopView()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubGameWelfareActiveCell.self,ClubOpenServerCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 100.0.Scale
        return tableView
    }()
    
    private var type = 0
    private var welfareDataSource = [ClubGameDetailWelfareModel]()
    private var openSourceData = [ClubOpenServiceModel]()
    
    var gid: String = ""{
        didSet{
            loadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        
        view.subviews{
            topView
            tableView
        }
        
        topView.leading(0).trailing(0).top(0).height(148.0.Scale)
        tableView.leading(0).trailing(0).bottom(0)
        tableView.Top == topView.Bottom
        topView.updateLayout = { isGift in
            if isGift {
                self.topView.heightConstraint?.constant = 148.0.Scale
            }else{
                self.topView.heightConstraint?.constant = 204.0.Scale
            }
            self.type = isGift ? 0 : 1
            self.loadData()
        }
        
        topView.giftBlock = {
            if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                let listVC = ClubGamesGiftListViewController()
                listVC.type = 0
                listVC.gameID = self.gid
                let url = RaeRequestParameterConfig.shared.baseUrlStr + "ct=app2021&ios=1&ac=lb_list&uid=" + uid + "&gid=" + self.gid
                listVC.urlString = url
                listVC.recordUrl = url
                self.navigationController?.pushViewController(listVC, animated: true)
            }else{
                self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
            }
        }
        topView.couponBlock = {
            if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                let listVC = ClubGamesGiftListViewController()
                listVC.type = 1
                listVC.gameID = self.gid
                let url = RaeRequestParameterConfig.shared.baseUrlStr + "ct=app2021&ios=1&ac=coupon1&uid=" + uid + "&gid=" + self.gid
                listVC.urlString = url
                listVC.recordUrl = url
                self.navigationController?.pushViewController(listVC, animated: true)
            }else{
                self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
            }
        }
    }

}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension ClubGameWelfareViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if type == 0 {
            return welfareDataSource.count
        }else{
            return openSourceData.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if type == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubGameWelfareActiveCell", for: indexPath) as! ClubGameWelfareActiveCell
            cell.model = welfareDataSource[indexPath.row]
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubOpenServerCell", for: indexPath) as! ClubOpenServerCell
            cell.model = openSourceData[indexPath.row]
            return cell
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if type == 0 {
            let id = welfareDataSource[indexPath.row].id
            let urlString = RaeRequestParameterConfig.shared.baseUrlStr + "ct=aznews&ac=info&az=1&id=\(id)"
            let webView = ClubWebViewController()
            webView.urlString = urlString
            self.navigationController?.pushViewController(webView, animated: true)
        }
    }

}

extension ClubGameWelfareViewController {
    
    private func loadData(){
        //福利列表
        ToastUtil.show()
        if type == 0 {
            Task {
                let res = await network.request(.activeList(gid: self.gid),type: ClubGameDetailWelfareModel.self)
                tableView.refresh_end()
                ToastUtil.dismiss()
                if res.isSuccess, let models = res.models {
                    
                    self.welfareDataSource = models
                    self.tableView.reloadData()
                }
            }
        }else{
            Task{
                let res = await network.requestSpecialData(.openService(gameId: self.gid), type: ClubOpenServiceModel.self, isShowError: false)
                tableView.refresh_end()
                ToastUtil.dismiss()
                if res.isSuccess, let models = res.models {
                    
                    self.openSourceData = models
                    self.tableView.reloadData()
                }
            }
        }
        
    }
}

extension ClubGameWelfareViewController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
